//
//  NewFeatureController.swift
//  weibo001
//
//  Created by ios on 15/11/6.
//  Copyright © 2015年 ios. All rights reserved.
//

import UIKit
import SnapKit

private let WBNewFeatureCellID = "WBNewFeatureCellID"

class NewFeatureController: UICollectionViewController {

 private let WBNewFeatureCount = 4
    
    init() {
        
    let layout = UICollectionViewFlowLayout()
        layout.itemSize = UIScreen.mainScreen().bounds.size
        layout.minimumLineSpacing = 0
        layout.minimumInteritemSpacing = 0
        layout.scrollDirection = .Horizontal
        
        super.init(collectionViewLayout: layout)
        
        collectionView?.backgroundColor = UIColor.whiteColor()
        collectionView?.pagingEnabled = true
        collectionView?.bounces = false
        collectionView?.showsHorizontalScrollIndicator = false
    }
    
    
    
   required init?(coder aDecoder: NSCoder) {
    
      fatalError("init(coder:) has not been implemented")
    }
    
    override func prefersStatusBarHidden() -> Bool {
        return true
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()

        self.collectionView!.registerClass(NewFeatureViewCell.self, forCellWithReuseIdentifier: WBNewFeatureCellID)
    
        
    }

   
   

  //数据源方法

   
    override func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        // #warning Incomplete implementation, return the number of items
        return WBNewFeatureCount
    }

    override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier(WBNewFeatureCellID, forIndexPath: indexPath) as! NewFeatureViewCell
    
        cell.imageIndex = indexPath.item
    
        return cell
        
    }
    
    override func scrollViewDidEndDecelerating(scrollView: UIScrollView) {
        
        let page = Int(scrollView.contentOffset.x / scrollView.bounds.width)
        
        if page != WBNewFeatureCount - 1 {
        return
        } else {
        
        
            (collectionView?.visibleCells().last as! NewFeatureViewCell).showStartButton()
        }
        
    }
    
//MARK:新特性 Cell
    
private class  NewFeatureViewCell: UICollectionViewCell {
    
        // 图像索引
        var imageIndex: Int = 0 {
            didSet {
            
               iconView.image = UIImage(named: "new_feature_\(imageIndex + 1)")
            
               startButton.hidden = true
            }
        }
  
   @objc private func clickStarButton() {
    
    NSNotificationCenter.defaultCenter().postNotificationName(WBSwitchRootViewControllerNotification, object: nil)
    
    }
    
    
    // 按钮动画
    
    private func showStartButton() {
      
        startButton.hidden = false
        
    startButton.transform = CGAffineTransformMakeScale(0, 0)
        
        UIView.animateWithDuration(2.0, delay: 0, usingSpringWithDamping: 0.8, initialSpringVelocity: 10, options: [], animations: { () -> Void in
            
            self.startButton.transform = CGAffineTransformIdentity
            
            }) { (_) -> Void in
                
        }
    }
    
    
        override init(frame: CGRect) {
            super.init(frame: frame)
            setupUI()
        }

        required init?(coder aDecoder: NSCoder) {
            fatalError("init(coder:) has not been implemented")
        }
    
    
        
        
        private func setupUI() {
            // 添加控件
        addSubview(iconView)
        addSubview(startButton)
            
        //自动布局
            iconView.frame = UIScreen.mainScreen().bounds
            
            startButton.snp_makeConstraints { (make) -> Void in
                make.centerX.equalTo(snp_centerX)
                make.bottom.equalTo(snp_bottom).multipliedBy(0.7)
                
            }
            
            startButton.addTarget(self, action: "clickStarButton", forControlEvents: .TouchUpInside)
        
        }
    
        //懒加载控件
        
        private lazy var iconView :UIImageView = UIImageView()
        
    private lazy var startButton :UIButton = UIButton(title:"开始体验", imageName: "new_feature_finish_button", color: UIColor.whiteColor())
    
    }
    
}

